package dataAccess;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import bussinesObjetcs.ContactoBean;


public class ContactoDAO {
//	ConnectionDB connectionDB;
	private PreparedStatement consultacontacto, insertacontacto;
	private Connection connection;
	
	public ContactoDAO(){
		
	}
	
	public ContactoDAO(Connection  connection){
      this.connection = connection;
	}

	public Connection getConnection(){
		return connection;
	}
		
	public void setConnection(Connection connection){
	   this.connection = connection;
	}
	
	public ArrayList<ContactoBean> getContactoList() {	
		    ArrayList<ContactoBean> contactoList = new ArrayList<ContactoBean>();
		    try {
		         consultacontacto = connection.prepareStatement("SELECT * FROM files.contactos");	   
		         // obteniendo la  lista de colegios
		         synchronized(consultacontacto) {
		            ResultSet results = consultacontacto.executeQuery();
		            // get rows data{
		            while (results.next()) {
		            	ContactoBean contacto = new ContactoBean();
			           
		               contacto.setNombrecompleto(results.getString("nombrecompleto"));
		               contacto.setEmail(results.getString("email"));
		               contacto.setAsunto(results.getString("asunto"));
		               contacto.setSugerencia(results.getString("sugerencia"));
		               			          
			          
		               contactoList.add(contacto);
		            }
		        }
		        consultacontacto.close();
		    } catch(SQLException sqle){
				 System.out.println(sqle);
			}	   
		    return contactoList;
	   }


	public ContactoBean searchUser(String email) {	
		ContactoBean contacto = null;
		    try {
		         consultacontacto = connection.prepareStatement("SELECT * FROM files.contactos WHERE email = ?");	   
		         consultacontacto.setString(1, email);
		         // obteniendo colegio
		         synchronized(consultacontacto) {
		            ResultSet results = consultacontacto.executeQuery();
		            // get rows data{
			  		  
		            while (results.next()) {	            	
			           contacto = new ContactoBean();
			           
			           contacto.setNombrecompleto(results.getString("nombrecompleto"));
			           contacto.setEmail(results.getString("email"));
			           contacto.setAsunto(results.getString("asunto"));
			           contacto.setSugerencia(results.getString("sugerencia"));
			          
		            }
		        }
		        consultacontacto.close();
		    } catch(SQLException sqle){
				 System.out.println(sqle);
			}	   
		    return contacto;
	   }
	 public void addUser (ContactoBean contacto)  {
		 try {
		   insertacontacto = connection.prepareStatement("INSERT INTO files.contactos VALUES(?,?,?,?)");
		   synchronized(insertacontacto) {
			   
			   
			   insertacontacto.setString(1, contacto.getNombrecompleto());
			   insertacontacto.setString(2, contacto.getEmail());
			   insertacontacto.setString(3, contacto.getAsunto());
			   insertacontacto.setString(4, contacto.getSugerencia());
			
			   insertacontacto.executeUpdate();
		   }
		   insertacontacto.close();
		 } catch(SQLException sqle){
			 System.out.println(sqle);
		 }
		   
	   }

}